package com.ibiaoke.customer.dao;

import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.exception.DaoException;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.customer.entity.CustomerExample;
import com.ibiaoke.customer.entity.vo.CustomerVO;
import com.ibiaoke.customer.mapper.CustomerMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/6/19.
 */
@Repository
public class CustomerDao extends BaseDao<Customer,CustomerExample,Integer,CustomerMapper> {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public CustomerMapper getMapper() {
        return customerMapper;
    }

    public List<Customer> listCustomer(String customerUsername, String customerNickname, String customerMobile, String customerEmail, Date startTime, Date endTime, Page page) {
        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(customerUsername)) {
            criteria.andCustomerUsernameLike(customerUsername);
        }
        if (StringUtils.isNotEmpty(customerNickname)) {
            criteria.andCustomerNicknameLike(customerNickname);
        }
        if (StringUtils.isNotEmpty(customerMobile)) {
            criteria.andCustomerMobileLike(customerMobile);
        }
        if (StringUtils.isNotEmpty(customerEmail)) {
            criteria.andCustomerEmailLike(customerEmail);
        }
        if (startTime != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(startTime);
        }

        if (endTime != null) {
            criteria.andCreateTimeLessThan(endTime);
        }
        criteria.andIsSellerEqualTo(0);
        criteria.andDelFlagEqualTo(0);

        page.setTotalCount(customerMapper.countByExample(example));
        example.setLimitStart(page.limitStart());
        example.setLimitEnd(page.limitEnd());
        example.setOrderByClause("create_time desc");
        return customerMapper.selectByExample(example);
    }


    public int updateCustomer(Integer customerId, String customerNickname, String customerMobile, String customerEmail) {
        Customer customer =new Customer();
        customer.setCustomerId(customerId);
        customer.setCustomerNickname(customerNickname);
        customer.setCustomerMobile(customerMobile);
        customer.setCustomerEmail(customerEmail);
        return customerMapper.updateByPrimaryKeySelective(customer);
    }

    public Customer getByName(String userName) {
        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria = example.createCriteria();
        criteria.andCustomerUsernameEqualTo(userName);
        List<Customer> customers = customerMapper.selectByExample(example);
        return CollectionUtils.isEmpty(customers)?null:customers.get(0);
    }

    public Customer getCustomer(String customerMobile) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustomerMobileEqualTo(customerMobile);
        return getByExample(example);
    }

    public Customer getByMobile(String mobile) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustomerMobileEqualTo(mobile);
        return getByExample(example);
    }

    public Customer loginByToken(String token) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andTokenEqualTo(token).andTokenExpireTimeGreaterThan(new Date());
        return getByExample(example);
    }

    public Integer updateCustomerPassword(String customerMobile,String password, String userSalt) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustomerMobileEqualTo(customerMobile);
        Customer customer = new Customer();
        customer.setCustomerPassword(password);
        customer.setUserSalt(userSalt);
        return updateByExampleSelective(customer, example);
    }

    public int updateCustomerInfo(Integer customerId,CustomerVO customerVO) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustomerIdEqualTo(customerId);

        Customer customer = new Customer();
        customer.setCustomerImg(customerVO.getCustomerImg());
        customer.setCustomerNickname(customerVO.getCustomerNickname());
        customer.setCustomerSex(customerVO.getCustomerSex());

        return customerMapper.updateByExampleSelective(customer, example);
    }

    public int changeCustomerNickname(Integer customerId, String nickName) {
        Customer customer = new Customer();
        customer.setCustomerNickname(nickName);
        customer.setCustomerId(customerId);
        return customerMapper.updateByPrimaryKeySelective(customer);
    }

    public int changeCustomerImg(Integer customerId, String headImageUrl) {
        Customer customer = new Customer();
        customer.setCustomerImg(headImageUrl);
        customer.setCustomerId(customerId);
        return customerMapper.updateByPrimaryKeySelective(customer);
    }

    public int updateCustomerInfos(Integer customerId, String headImageUrl, String nickName) {
        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(headImageUrl)) {
            criteria.andCustomerImgEqualTo(headImageUrl);
        }
        if (StringUtils.isNotEmpty(nickName)) {
            criteria.andCustomerNicknameEqualTo(nickName);
        }
        Customer customer = new Customer();
        customer.setCustomerId(customerId);
        return customerMapper.updateByExampleSelective(customer,example);
    }

    public int editUserMobile(Integer customerId, String customerMobile) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustomerIdEqualTo(customerId);

        Customer customer = new Customer();
        customer.setCustomerMobile(customerMobile);
        return customerMapper.updateByExampleSelective(customer,example);
    }

    public Customer getCustomerById(Integer id){
        return selectByPrimaryKey(id);
    }
}
